package com.cooldatasoft.web.cms.user;

import java.util.Date;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.app.WOCSession;
import com.cooldatasoft.base.BaseViewPage;
import com.cooldatasoft.data.User;

public class UserViewPage extends BaseViewPage{

	
	private FeedbackPanel feedbackPanel;
	
	public UserViewPage(Integer entityId) {
		
		
		MarkupContainer container= new WebMarkupContainer("container");		
		MarkupContainer header = new WebMarkupContainer("header");
		ExternalLink companyWebsiteLink = new ExternalLink("companyWebsiteLink","http://www.cooldatasoft.com","Company Name");
		companyWebsiteLink.add(new AttributeModifier("target",true,new Model("_blank")));
		header.add(companyWebsiteLink);
		container.add(header);
		
		
		MarkupContainer wrapper = new WebMarkupContainer("wrapper");
		MarkupContainer personalInfoDiv = new WebMarkupContainer("personalInfoDiv");
		PersonPropertyForm personalInfoForm = new PersonPropertyForm("personalInfoForm",entityId);
		personalInfoDiv.add(personalInfoForm);
		wrapper.add(personalInfoDiv);
		
		container.add(wrapper);
		
		add(container);
	}

	
	class PersonPropertyForm extends Form{

		private User user;
		
		public PersonPropertyForm(String id,Integer entityId) {
			super(id);
			if(entityId!=null){
				user = WOCApplication.get().getUserServiceProxy().getUserById(entityId);
			}
			Label idLabel = new Label("idLabel","Id");
			Label usernameLabel = new Label("usernameLabel","Username");
			Label passwordLabel = new Label("passwordLabel","Password");
			
			add(idLabel);
			add(usernameLabel);
			add(passwordLabel);
			
			
			Label idValue = new Label("idValue",new PropertyModel(this,"user.id"));
			TextField usernameTextField = new TextField("usernameTextField",new PropertyModel(this,"user.username"));
			TextField passwordTextField = new TextField("passwordTextField",new PropertyModel(this,"user.password"));
		
			add(idValue);
			add(usernameTextField);
			add(passwordTextField);
			
			Button personalInfoSaveButton = new Button("saveButton");
			add(personalInfoSaveButton);			
		}

		@Override
		protected void onSubmit() {
			user.setCreateUserId(WOCSession.get().getUser().getId());
			user.setUpdateUserId(WOCSession.get().getUser().getId());
			user.setUpdateDate(new Date());
			WOCApplication.get().getUserServiceProxy().saveOrUpdate(user);
			setResponsePage(UserListPage.class);
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

		
	}
}
